<?php
namespace app\api\controller;
use app\model\Exam as ExamModel;
use app\model\Problem as ProblemModel;
use think\Db;


class Exam extends Base
{
    protected $noCheckToken = ['ranking'];

    public function items(){
        $user_id = $this->login_user['user_id'];
        $map = [];
        if ( isSearchParam('ctime') ) {
            $map[] = ['ctime','=',input('ctime')];
        }else{
            $map[] = ['ctime','=',date('Y-m-d')];
        }
        $map[] = ['user_id','=',$user_id];

        $list = Db::name('exam_item')->where($map)->select();
        foreach ($list as $k => $vo) {
            $problem = Db::name('problem')->where('id',$vo['problem_id'])->find();
            $problem['content'] = json_decode($problem['content'],true);
            $list[$k]['problem'] = $problem;
        }

        return apiResult('2000','获取成功',$list);
    }

    public function read(){
        $user_id = $this->login_user['user_id'];
        $map = [];
        if ( isSearchParam('ctime') ) {
            $map[] = ['ctime','=',input('ctime')];
        }else{
            $map[] = ['ctime','=',date('Y-m-d')];
        }
        $map[] = ['user_id','=',$user_id];

        $model = new ExamModel();
        $data = $model->where($map)->find();
        if (empty($data)) {
            $data = [
                'ctime'=>date('Y-m-d'),
                'total_num'=>0,
                'total_snum'=>0,
                'total_enum'=>0
            ];
        }else{
            $data['total_num'] = $data['total_snum'] + $data['total_enum'];
        }


        return apiResult('2000','获取成功',$data);
    }

    /**
     * @Author   CCH
     * @DateTime 2020-05-23T12:10:10+0800
     * @return   数据列表
     */
    public function ranking(){
        $map_params = [
            
        ];
        $map = getSearchParam($map_params);
        if ( isSearchParam('ctime') ) {
            $map[] = ['ctime','=',input('ctime')];
        }else{
            $map[] = ['ctime','=',date('Y-m-d')];
        }


        $model = new ExamModel();
        $list = $model->where($map)->order('total_snum','desc')->limit(20)->select();
        $titles = [
            '最强王者','傲视宗师','超凡大师','璀璨钻石','华贵铂金','荣耀黄金','不屈白银','英勇黄铜','坚韧黑铁'
        ];

        foreach ($list as $k => $vo) {
            $total_num = $vo['total_snum'] + $vo['total_enum'];
            $list['data'][$k]['total_num'] = $total_num;
            $list['data'][$k]['accuracy'] = (round($vo['total_snum'] / $total_num,2) * 100).'%';

            $list[$k]['title'] = emptyDef($titles[$k],'暂无段位');

            $list[$k]['user'] = Db::name('user')->field('id,nickname,headimgurl')->where('id',$vo['user_id'])->find();
        }
        return apiResult('2000','获取成功',$list);
    }
    
    /**
     * @Author   CCH
     * @DateTime 2020-05-23T12:10:10+0800
     * @return   数据列表
     */
    public function index(){
        $user_id = $this->login_user['user_id'];
        $map_params = [
     
        ];
        $map = getSearchParam($map_params);
        $map[] = ['user_id','=',$user_id];


        $model = new ExamModel();
        $page_param = ['page'=>input('page_index',1),'list_rows'=>input('page_size',10)];
        $list = $model->where($map)->order('id','desc')->paginate($page_param)->toarray();
        foreach ($list['data'] as $k => $vo) {
            $total_num = $vo['total_snum'] + $vo['total_enum'];
            $list['data'][$k]['total_num'] = $total_num;
            $list['data'][$k]['accuracy'] = (round($vo['total_snum'] / $total_num,2) * 100).'%';
        }
        return apiResult('2000','获取成功',$list);
    }
   

    public function submit($problem_id,$answer){
        $user_id = $this->login_user['user_id'];
        $curdate = date('Y-m-d');
        // if (Db::name('exam')->where([
        //     ['user_id','=',$user_id],
        //     ['ctime','=',$curdate]
        // ])->value('id') > 0) {
        //     return json(['code'=>5000,'msg'=>'今日已答过题']);
        // }
        $problem = Db::name('problem')->where('id',$problem_id)->find();

        // $answer = sort($answer);
        // $answer = implode(',',$answer);

        $save_data = [
            'user_id'=>$user_id,
            'problem_id'=>$problem['id'],
            'problem_bank_id'=>$problem['problem_bank_id'],
            'answer'=>$answer,
            'ctime'=>$curdate,
            'category'=>$problem['category'],
            'status'=>$problem['answer'] == $answer ? 1 : 0
        ];
        // 保存明细
        Db::name('exam_item')->insert($save_data);

        // 保存每日统计
        $exam_id = Db::name('exam')->where([
            ['user_id','=',$save_data['user_id']],
            ['ctime','=',$save_data['ctime']]
        ])->value('id');
        if ( empty($exam_id) ) {
            $exam_id = Db::name('exam')->insertGetId([
                'user_id'=>$save_data['user_id'],
                'ctime'=>$save_data['ctime']
            ]);
        }
        $exam_fields = [];
        $category = array_search($save_data['category'], ProblemModel::getCategoryList());
        if ($category === false) {
            $category = 0;
        }
        if ($save_data['status'] == 1) {
            $exam_fields[] = 'total_snum';
            $exam_fields[] = 'category'.$category.'_snum';
        }else{
            $exam_fields[] = 'total_enum';
            $exam_fields[] = 'category'.$category.'_enum';
        }
        $model = Db::name('exam');
        $model->where('id',$exam_id);
        foreach ($exam_fields as $k => $vo) {
            $model->inc($vo);
        }
        $model->update();

        return apiResult('2000','获取成功',[
            'status'=>$save_data['status'],
            'answer'=>explode(',', $problem['answer']),
            'analysis'=>emptyDef($problem['analysis'],''),
        ]);
    }


   
}
